package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品信息到dish表中
        this.save(dishDto);
        //菜品id
        Long id = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //保存菜品口味数据到菜品口味表dish_flavor
        flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        /*  1、根据id查询菜品的基本信息，dish表*/
        Dish dish = this.getById(id);
        //拷贝对象
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        /*  2、查询当前菜品对应的口味信息，从dish_flavor表查询*/
        //2.1、构造建查询条件构造器
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //2.2、配置查询条件
        lqw.eq(DishFlavor::getDishId, dish.getId());
        //2.3、执行查询，获得口味信息
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        //2.4、将口味信息赋值给dishDto对象
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        /* 1、 更新dish表基本信息*/
        this.updateById(dishDto);

        /* 2、清理当前菜品对应口味数据---dish_flavor表的delete操作*/
        //2.1、构建查询条件构造器
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //2.2、配置查询条件
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        //2.3、执行查询操作
        dishFlavorService.remove(lqw);

        /*/3、添加当前提交过来的口味数据---dish_flavor表的insert操作*/
        //3.1、获取当前提交的口味数据列表
        List<DishFlavor> flavors = dishDto.getFlavors();
        //3.2、为口味列表中的每一个口味对象的dishid属性，赋值
        flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override//逻辑删除
    public void deleteWithFlavor(List<Long> ids) {
        /*1、在dish表中，先删除基本信息*/
        this.removeByIds(ids);
        /*2、在dish_flavor表中，先删除口味信息*/
        //2.1、构建条件构造器
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //2.2、配置条件
        lqw.in(DishFlavor::getDishId, ids);
        //2.3、执行条件
        dishFlavorService.remove(lqw);

    }
}
